﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Hrm.Core;
using Hrm.EntityFramework;
using Hrm.Services;

namespace Hrm.Web.Areas.Studio.Controllers
{
    public class ServicesController : Controller
    {
        private readonly IServiceService _serviceService;
        private readonly IDictionaryService _dictionaryService;
        public ServicesController(IServiceService serviceService, IDictionaryService dictionaryService)
        {
            this._serviceService = serviceService;
            this._dictionaryService = dictionaryService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult PageService(PageRequest<ServiceDto> request)
        {
            var data = this._serviceService.GetPageList(request);
            return Json(data);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,Description,Glyphicon")] Service service)
        {
            if (ModelState.IsValid)
            {
                _serviceService.Insert(service);
                return RedirectToAction("Index");
            }

            return View(service);
        }

        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Service service = _serviceService.GetById(id);
            if (service == null)
            {
                return HttpNotFound();
            }
            return View(service);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Service service)
        {
            if (ModelState.IsValid)
            {
                Service model = _serviceService.GetById(service.Id);
                if (TryUpdateModel(model, "", new[] { "Name", "Description", "Glyphicon" }))
                    _serviceService.Update(model);
                return RedirectToAction("Index");
            }
            return View(service);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public JsonResult DeleteConfirmed(int id)
        {
            ResponseModel result = new ResponseModel();
            if (id == 0)
            {
                result.SetFailed("删除失败，参数不能为空！");
                return Json(result);
            }
            var resource = this._serviceService.GetById(id);
            if (resource == null)
            {
                result.SetFailed("删除失败，查询没有改数据！");
                return Json(result);
            }
            this._serviceService.UpdateDisabled(resource);
            return Json(result);
        }

        public ActionResult PriceTypeItem()
        {
            var list = this._serviceService.GetAllPriceType();
            return View(list);
        }

        public ActionResult AddPriceType()
        {
            //ViewBag.BelongId = this._dictionaryService.GetDictionaryByType(Constant.DictionaryContactType); 
            return View(new PriceType());
        }

        [HttpPost]
        public ActionResult AddPriceType(PriceType model)
        {
            var result = this._serviceService.AddPriceType(model);
            if (result)
                return RedirectToAction("PriceTypeItem");
            return View();
        }

        public ActionResult AddPriceItem(int id)
        {
            ViewBag.PriceTypeId = id;
            return View();
        }

        [HttpPost]
        public ActionResult AddPriceItem(PriceItem model, int priceTypeId)
        {
            model.Id = 0;
            model.PriceType = this._serviceService.GetPriceType(priceTypeId);
            var result = this._serviceService.AddPriceItem(model);
            if (result)
                return RedirectToAction("PriceTypeItem");
            ViewBag.PriceTypeId = priceTypeId;
            return View();
        }

        public ActionResult UpdatePriceType(int id)
        {
            var result = this._serviceService.GetPriceType(id);
            return View(result);
        }

        [HttpPost]
        public ActionResult UpdatePriceType(PriceType model)
        {
            var updateModel = this._serviceService.GetPriceType(model.Id);
            if (TryUpdateModel(updateModel, "", new[] { "Id", "Name", "Description", "BelongId", "BelongType", "Price", "Unit" }))
            {
                this._serviceService.UpdatePriceType(updateModel);
            }
            return RedirectToAction("PriceTypeItem");
        }

        [HttpPost]
        public JsonResult DeletePriceItem(int id)
        {
            var result = this._serviceService.DeletePriceItem(id);
            return Json(result);
        }

        [HttpPost]
        public JsonResult DeletePriceType(int id)
        {
            var result = this._serviceService.DeletePriceType(id);
            return Json(result);
        }
    }
}
